﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace Card.Manager
{
    public partial class SiteManager : BasePage
    {
        private int AreaID
        {
            get
            {
                if (Request.QueryString["id"] != null)
                {
                    return int.Parse(Request.QueryString["id"]);
                }
                return 0;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.DoInit();
            }
        }

        protected override void OnInit(EventArgs e)
        {
            this.userPicker.OnUserPicker += new Card.Manager.Controls.UserPicker.PickerUser(userPicker_OnUserPicker);
            base.OnInit(e);
        }

        void userPicker_OnUserPicker(int userID)
        {
            BLL.SiteManagerMangager.Save(new Card.Entity.SiteManager()
            {
                UserID = userID,
                SiteCode = this.ltlSiteCode.Text
            }, this.CPassport);

            this.DoBindManager();
        }

        protected void rptManager_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "DELETE")
            {
                BLL.SiteManagerMangager.Delete(int.Parse(e.CommandArgument.ToString()), this.ltlSiteCode.Text, this.CPassport);
            }
            this.DoBindManager();
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("AreaManager.aspx");
        }

        private void DoInit()
        {
            Entity.Area area = BLL.AreaManager.GetEntity(this.AreaID, this.CPassport);
            this.ltlID.Text = area.ID.ToString();
            this.ltlSiteName.Text = area.Name;
            this.ltlSiteCode.Text = area.SiteCode;
            this.ltlSiteUrl.Text = area.SiteUrl;
            if (string.Compare(area.IsActive, CONST.YES_NO.YES) != 0)
            {
                this.form1.Visible = false;
                this.MessageBox("该地区分站还没有开通，请先开通分站！", string.Format("AreaEdit.aspx?id={0}", this.AreaID));
            }
            this.DoBindManager();
        }

        private void DoBindManager()
        {
            DataTable dtData = BLL.SiteManagerMangager.GetManagerByAreaCode(this.ltlSiteCode.Text, this.CPassport);
            this.userPicker.ExcludeUserIDs = string.Join(",", (from row in dtData.AsEnumerable()
                                                               select row["USER_ID"].ToString()).ToArray());

            this.rptManager.DataSource = dtData;
            this.rptManager.DataBind();
        }
    }
}
